<?php
/**
 * Created by PhpStorm.
 * User: XIESHUAI
 * Date: 2017/8/25
 * Time: 22:11
 */

namespace Core\Lib;

class DbMysqlExt extends DbMysql
{
    private $sql_table = array();
    private $sql_table_str = "";
    private $sql_where = array();
    private $sql_where_str = "";
    private $sql_field = array();
    private $sql_field_str = "";
    private $sql_values = array();
    private $sql_values_str = "";
    private $sql_orderby = array();
    private $sql_orderby_str = "";
    private $sql_groupby = array();
    private $sql_groupby_str = "";

    private $sql_sql = "";

    private $last_sql = "";

    function __construct($sn)
    {
        if (is_array($sn)) {

        }
        //parent::__construct($host, $user, $passwd, $dbname, $port);
    }

    function __destruct()
    {
        parent::__destruct(); // TODO: Change the autogenerated stub
    }

    function clear_all()
    {

        return $this;
    }

    function table($tbName)
    {
        if (is_array($tbName)) {
            $this->sql_table = array_merge($this->sql_table, $tbName);
        } else if (is_string($tbName)) {
            $this->sql_table[] = $tbName;
        }
        return $this;
    }

    function where($arg1, $relation, $arg2)
    {
        if (!is_object($arg1) && !is_array($arg1) && is_string($relation) && !is_object($arg2) && !is_array($arg2)) {
            $this->sql_where[] = array($arg1, $relation, $arg2);
        }
        return $this;
    }

    function field($fName)
    {
        if (is_array($fName)) {
            $this->sql_field = array_merge($this->sql_field, $fName);
        } else if (is_string($fName)) {
            $this->sql_field[] = $fName;
        }
        return $this;
    }

    function value($keyvalue)
    {
        if (is_array($keyvalue)) {
            $this->sql_values = array_merge($this->sql_values, $keyvalue);
        }
        return $this;
    }

    function orderby($field, $desc = false)
    {
        if (is_string($field) && is_bool($desc)) {
            $this->sql_orderby[] = array($field, $desc);
        }
        return $this;
    }

    function groupby($field)
    {
        return $this;
    }

    function prepare_sql($action)
    {
        if (count($this->sql_field) > 0) {
            $this->sql_field_str = "`" . implode("`,`", $this->sql_field) . "`";
        } else {
            $this->sql_field_str = "*";
        }
        $this->sql_table_str = "`" . implode("`,`", $this->sql_table) . "`";
        switch ($action) {
            case "select":
                break;
            case "insert":
                break;
            case "update":
                break;
            case "delete":
                break;
            default:
                return "";
        }
        return $this->sql_sql;
    }
}